package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.mapper.*;
import com.chen.pojo.common.PageResult;
import com.chen.pojo.dto.VmDto;
import com.chen.pojo.entry.*;
import com.chen.pojo.vo.VmVo;
import com.chen.service.VmService;
import com.chen.utils.GeocodeUtils;
import com.chen.utils.CodeUtils;
import com.chen.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/8/20
 * 设备管理业务实现类
 */
@Service
@Transactional
public class VmServiceImpl implements VmService {
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private VmPolicyMapper vmPolicyMapper;
    @Autowired
    private PolicyMapper policyMapper;

    /**
     * 分页查询
     *
     * @param pageIndex 起始页
     * @param pageSize  页大小
     * @param innerCode 设备编号
     * @return 结果
     */
    @Override
    public PageResult<VmVo> searchList(Integer pageIndex, Integer pageSize, String innerCode) {
        Page<VmVo> page = new Page<>(pageIndex, pageSize);
        Map<String, Boolean> map = new HashMap<>();
        map.put("10002", true);
        map.put("10001", false);
        map.put("10003", true);
        page = vendingMachineMapper.selectPageList(page, innerCode);
        List<VmVo> records = page.getRecords();
        records.forEach(s ->
                s.setStatus(map)
        );
        return PageUtils.mapperResult(page);
    }

    /**
     * 新增设备
     *
     * @param vmDto 设备信息
     * @return 结果
     */
    @Override
    public Boolean addVm(VmDto vmDto) {
        // 获取点位信息
        Node node = nodeMapper.selectById(vmDto.getNodeId());
        // 获取创建用户的信息
        User user = userMapper.selectById(vmDto.getCreateUserId());
        // 获取城市信息
        LambdaQueryWrapper<Area> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(node.getAreaCode()), Area::getAdCode, node.getAreaCode());
        Area area = areaMapper.selectOne(wrapper);

        VendingMachine vm = new VendingMachine();
        // 基础信息
        vm.setNodeId(vmDto.getNodeId());
        vm.setVmType(vmDto.getVmType());
        vm.setCreateUserId(vmDto.getCreateUserId());
        vm.setInnerCode(CodeUtils.getInnerCode());

        // 所在城市id
        vm.setCityCode(area.getCityCode());
        vm.setAreaId(area.getId());

        // 创建人姓名
        vm.setCreateUserName(user.getLoginName());

        // 点位信息
        vm.setBusinessId(node.getBusinessId());
        vm.setRegionId(node.getRegionId());
        vm.setOwnerId(node.getOwnerId());
        vm.setOwnerName(node.getOwnerName());

        // 客户端连接id
        vm.setClientId(vmDto.getCreateUserId());
        // 经纬度
        vm.setLatitude(GeocodeUtils.getLatitude(node.getAddr()));
        vm.setLongitudes(GeocodeUtils.getLongitude(node.getAddr()));

        vm.setUpdateTime(LocalDateTime.now());
        vm.setCreateTime(LocalDateTime.now());
        vendingMachineMapper.insert(vm);
        return true;
    }

    /**
     * 修改售货机点位信息
     *
     * @param vmId   售货机id
     * @param nodeId 点位id
     * @return 结果
     */
    @Override
    public Boolean updateVm(String vmId, String nodeId) {
        VendingMachine vm = new VendingMachine();
        vm.setId(vmId);
        Node node = nodeMapper.selectById(nodeId);
        Area area = areaMapper.selectById(node.getAreaCode());
        // nodeid
        vm.setNodeId(nodeId);
        // regionId
        vm.setRegionId(node.getRegionId());
        // ownerid,name
        vm.setOwnerId(node.getOwnerId());
        vm.setOwnerName(node.getOwnerName());
        // businessId
        vm.setBusinessId(node.getBusinessId());
        // cityCode
        vm.setCityCode(area.getCityCode());
        // area_id
        vm.setAreaId(area.getId());
        // 经纬度
        vm.setLatitude(GeocodeUtils.getLatitude(node.getAddr()));
        vm.setLongitudes(GeocodeUtils.getLongitude(node.getAddr()));
        vendingMachineMapper.updateById(vm);
        return true;
    }

    /**
     * 策略配置
     *
     * @param vmDto
     * @return
     */
    @Override
    public Boolean applyPolicy(VmDto vmDto) {
        List<VmPolicy> targetList = new ArrayList<>();
        Policy policy = policyMapper.selectById(vmDto.getPolicyId());
        List<String> innerCodeList = vmDto.getInnerCodeList();
        if (!CollectionUtils.isEmpty(innerCodeList)) {
            innerCodeList.forEach(s -> {
                VmPolicy vmPolicy = new VmPolicy();
                BeanUtils.copyProperties(policy, vmPolicy);
                vmPolicy.setInnerCode(s);
                targetList.add(vmPolicy);
            });
        }
        vmPolicyMapper.insert(targetList);
        return true;
    }
}
